package com.zzyl.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
public class PDFUtil {
    static {
        // 禁用所有字体相关日志 - 终极解决方案
        System.setProperty("org.apache.commons.logging.Log",
                "org.apache.commons.logging.impl.NoOpLog");
    }

    private PDFUtil() {
        throw new UnsupportedOperationException("工具类不允许实例化");
    }

    public static String pdfToString(InputStream is) {
        try {
            PDDocument document= Loader.loadPDF(is.readAllBytes());
            PDFTextStripper pdfTextStripper = new PDFTextStripper();
            return pdfTextStripper.getText(document);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        FileInputStream fileInputStream = new FileInputStream("D:\\data\\体检报告-张芳-女-72岁.pdf");
        System.out.println(PDFUtil.pdfToString(fileInputStream));
    }
}